package com.cloudlead.customerAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.TbBookingSpaceOrderService;
import com.cloudlead.commonData.service.TbCustomerOrderService;
import com.cloudlead.commonData.vo.TbBookingSpaceOrder;
import com.cloudlead.commonData.vo.TbCustomerOrder;
import com.cloudlead.customerAction.service.BookingSpaceOrderFindSer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wyj on 2015/8/31.
 */
@Service
public class BookingSpaceOrderFindSerImpl implements BookingSpaceOrderFindSer {

    @Autowired
    TbBookingSpaceOrderService tbBookingSpaceOrderService;
    @Autowired
    TbCustomerOrderService tbCustomerOrderService;
    @Override
    public Object bookingSpaceOrderFind(String customerOrderId,String id) {
        TbBookingSpaceOrder tbso=new TbBookingSpaceOrder();
        if(id!=null&&(!"".equals(id))){//ID
            tbso.setId(id);
        }
        if(customerOrderId!=null&&("".equals(customerOrderId))) {//订单ID
            TbCustomerOrder tco = new TbCustomerOrder();
            tco.setId(customerOrderId);
            tbso.setTbCustomerOrder(tco);
        }
        tbso=tbBookingSpaceOrderService.findOne(tbso);//实例查询订舱单
        if(tbso!=null){
            if(tbso.getTbCustomerOrder()!=null&&tbso.getTbCustomerOrder().getId()!=null){
                TbCustomerOrder tcox=tbCustomerOrderService.view(tbso.getTbCustomerOrder().getId());//查询对应订单
                tbso.setTbCustomerOrder(tcox);//替换订单
            }
        }
        return tbso;
    }

    @Override
    public Page<Object> bookingSpaceOrderFindAll(String companyId,int size,int page) {
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();
        tr.add(new Term("flag","0"));//未删除
        //查询条件
        if(companyId!=null&&(!"".equals(companyId))) {
            tr.add(new Term("tbCustomerOrder.baseCompanyId.",companyId));//订单ID
        }
        Filter filter=new Filter( new Criteria( tr));
        pr.setPage(page);//页码
        pr.setSize(size);//每页数量
        pr.setFilter(filter);
        Sort sort = new Sort();//订单创建时间倒序
            sort.add(new Sort.Order(Sort.Direction.DESC, "createTime"));
            pr.setSort(sort);
        Page<TbBookingSpaceOrder> px =tbBookingSpaceOrderService.findAll(pr);
        List<TbBookingSpaceOrder> lpx=px.getContent();
        List<TbBookingSpaceOrder> lpx1= new ArrayList<TbBookingSpaceOrder>();
        for(Iterator<TbBookingSpaceOrder> it = px.iterator();it.hasNext();) {//遍历
            TbBookingSpaceOrder bso = it.next();
            if(bso.getTbCustomerOrder()!=null&&bso.getTbCustomerOrder().getId()!=null)
                bso.setTbCustomerOrder(tbCustomerOrderService.view(bso.getTbCustomerOrder().getId()));
            lpx1.add(bso);
        }
        px.setContent(lpx1);
        Page<Object> po = new  Page<Object>();
        BeanUtils.copyProperties(px, po);//拷贝page
        return po;
    }

    @Override
    //订舱单状态的修改
    public String bookingSpaceOrderCreate(String id, String orderStatus) {
        TbBookingSpaceOrder tbso=tbBookingSpaceOrderService.view(id);
        if(tbso==null){
            return "ID is error";
        }else {
            tbso.setOrderStatus(orderStatus);
            tbBookingSpaceOrderService.update(tbso);
            return "success";
        }
    }
}
